# -*- coding: utf-8 -*-
import urllib
import re

import scrapy
from scrapy.http import Request
from ..items import BlogyuanItem


class BlogsSpider(scrapy.Spider):
    name = 'blogs'
    allowed_domains = ['www.cnblogs.com']

    def start_requests(self):
        # url = "http://www.cnblogs.com/wupeiqi/articles/7805382.html"
        url = "http://www.cnblogs.com/wupeiqi/p/7295372.html"
        # url = "http://www.cnblogs.com/alex3714/articles/7550940.html"

        yield Request(url, callback=self.parse_next)

    def parse_next(self, response):

        # 保存应该保存的

        item = BlogyuanItem()
        item["title"] = response.xpath('//*[@id="cb_post_title_url"]/text()').extract()[0]
        item["url"] = response.xpath('//*[@id="cb_post_title_url"]/@href').extract()[0]
        item["creat_time"] = response.xpath('//*[@id="post-date"]/text()').extract()[0]
        item["text"] = response.xpath('//*[@id="topics"]/div/div[2]').extract()
        yield item

        # 获取上一页的url
        pre_info_url = self.get_pre_url(response.text)  # 通过本url可以获取到上一篇文章正真的url
        if pre_info_url:
            # print("pre_url", pre_info_url)
            yield Request(pre_info_url, callback=self.parse_pre)

    def parse_pre(self, response):
        """解析response中的prev_url,再发请求"""
        pre_url = re.findall(r'上一篇：<a href="(.*?)"', response.text, re.S)
        if pre_url:
            yield Request(pre_url[0], callback=self.parse_next)

    def get_pre_url(self, content):
        import re

        res = re.findall(
            r"<script.*?cb_blogId=(.*?),.*?cb_entryId=(.*?),.*?cb_entryCreatedDate='(.*?)';.*?cb_postType=(.*?);",
            content, re.S)
        if not res:
            return
        blogId, postId, dateCreated, postType = res[0]

        r = urllib.parse.urlencode({'postId': postId,
                                    'blogId': blogId,
                                    'dateCreated': dateCreated,
                                    "postType": postType
                                    })

        url = "http://www.cnblogs.com/post/prevnext?" + r
        return url
